package com.neutech.solar.entity;

import com.neutech.solar.contant.Constant;
import com.neutech.solar.util.ImageUtil;

import java.awt.*;

public class Planet extends Star{
    public Star star;

    public Planet(){

     }
     public Planet(Star star,String imageKey,double Au,double e,int T,String name){
         this.star = star;
        this.img = ImageUtil.maps.get(imageKey);
         this.width = img.getWidth(null);
         this.height = img.getHeight(null);
         this.longAxis = getLongAxisByAu(Au);
         this.shortAxis = getShortAxisByE(e);
         this.x = Constant.WINDOW_WIDTH / 2 + longAxis;
         this.y = Constant.WINDOW_HEIGHT / 2 ;
         this.theta = 0.0;
         this.speed = getSpeedByT(T);
         this.name = name;
    }
    public Planet(Sun sun,String imageName,double Au,double e,int T,String name,boolean smallPlanet){
       this(sun,imageName,Au,e,T,name);
        this.smallPlanet = smallPlanet;
    }
    public Planet(Sun sun,String imageName,double Au,double e,int T,String name,boolean smallPlanet,boolean venus){
        this(sun,imageName,Au,e,T,name,smallPlanet);
        this.venus = venus;
    }
     private int getLongAxisByAu(double Au){
        return (int) (100 * Au);
     }
     private int getShortAxisByE(double e){
        return (int)(this.longAxis * Math.sqrt(1 - e * e)) / 2;
     }
     private double getSpeedByT(int t){
        return 0.1 * 365 / t;
     }

    @Override
    public void draw(Graphics g) {

        Color c = g.getColor();
        g.setColor(Color.WHITE);
        if(!smallPlanet) {
            drawTrace(g);
        }
        Font f = g.getFont();
        g.setFont(new Font("幼圆",Font.BOLD,24));
        g.drawString(name,x,y - 30);
        g.setColor(c);
        g.drawImage(img,x,y,null);
        move();
    }
    private void drawTrace(Graphics g){
        int x = (int)(this.star.x - this.longAxis + star.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
        int y = this.star.y - this.shortAxis + star.height / 2 ;
        int width = 2 * this.longAxis;
        int height = 2 * this.shortAxis;
        g.drawOval(x,y,width,height);
    }

    @Override
    public void move() {
        if(!venus) {
            x = (int) ((longAxis * Math.sin(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
            y = (int) (shortAxis * Math.cos(theta)) + star.y + star.height / 2 - this.height / 2;
            theta += speed;
        }else{
            x = (int) ((longAxis * Math.cos(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
            y = (int) (shortAxis * Math.sin(theta)) + star.y + star.height / 2 - this.height / 2;
        }
    theta += speed;
    }
}
